/*
 * Sapphire backend
 *
 * Copyright (C) 2018 Alyssa Rosenzweig
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 */

#include "push.h"
#include "websocket.h"

/* Has a push notification been sent since the last login? We default to TRUE
 * to avoid sporadic push notifications before the first connection*/

gboolean sapphire_already_pushed = TRUE;

/* We got a connection, so we're clear for another push */

void
sapphire_push_connected(void)
{
	sapphire_already_pushed = FALSE;
}

/* Send a push notification with the given content. This wrapper function
 * determines whether the notification should actually be pushed, handling
 * state appropriately. Actual medium-specific sending is delegated. */

static void sapphire_push_notification_raw(const gchar *content);

void
sapphire_push_notification(const gchar *content)
{
	/* Push notifications can only be sent if:
	 *
	 * 	0. Push notifications are enabled (TODO)
	 * 	1. There are no connected clients
	 * 	2. There has not been another push notification sent
	 *
	 * Check these criteria
	 */

	if (sapphire_any_connected_clients()) return;
	if (sapphire_already_pushed) return;

	/* We've passed the criteria, so send the notification */

	sapphire_push_notification_raw(content);

	/* Record that we pushed something */
	sapphire_already_pushed = TRUE;
}

/* Actually send the push notification (medium-specific). At the moment, push
 * notifications are email-based, allowing for SMS beeps via SMS-email bridge,
 * etc */

static
void sapphire_push_notification_raw(const gchar *content)
{
	/* Send push notification as the body of an email via mailx. First,
	 * write the content to a file */

	const char *temp_file = "/dev/shm/push-notification.txt";
	FILE *fp = fopen(temp_file, "w");
	fputs(content, fp);
	fclose(fp);

	/* Find the target mail address */
	const char *address = purple_prefs_get_string(SAPPHIRE_PUSH_EMAIL_PREF);

	/* Strip whitespace so it's zero length if blank */
	gchar *stripped = g_strstrip(g_strdup(address));

	if (stripped[0] == '\0') {
		printf("Can't do push notifications to empty address, thonk\n");
		return;
	}

	/* Now, invoke mailx via system. XXX: SECURITY: Sanitize the address */
	gchar *command = g_strdup_printf("mailx -- %s < %s", stripped, temp_file);
	printf("Invoking %s\n", command);
	system(command);
	g_free(command);
	g_free(stripped);
	printf("Pushing %s\n", content);
}
